<template>
  <div class="account-settings-info-view">
    <a-row :gutter="16">
      <a-col :md="24" :lg="16">
        <a-form layout="vertical" :form="form">
          <a-form-item label="昵称">
            <a-input placeholder="给自己起个昵称吧" v-decorator="['name']"/>
          </a-form-item>
          <a-form-item label="英文名">
            <a-input placeholder="给自己起个英文名吧" v-decorator="['nameEn']"/>
          </a-form-item>
          <a-form-item label="个人介绍">
            <a-textarea rows="4" placeholder="您没有个人介绍" v-decorator="['remark']"/>
          </a-form-item>
          <a-form-item label="工作电话">
            <a-input placeholder="您没有填写座机" v-decorator="['officeTel', {rules: validatorRules.officeTel}]"/>
          </a-form-item>
          <a-form-item label="传真">
            <a-input placeholder="您没有填写传真" v-decorator="['fax', {rules: validatorRules.fax}]"/>
          </a-form-item>
          <a-form-item label="生日">
            <a-date-picker :format="'yyyy-MM-DD'" placeholder="生日" v-decorator="['birthday']" style="width:100%;"/>
          </a-form-item>
          <a-form-item label="性别">
            <a-select placeholder="请选择性别" v-decorator="['sex']">
              <a-select-option value="">不公开</a-select-option>
              <a-select-option value="0">男</a-select-option>
              <a-select-option value="1">女</a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" @click="base">保存</a-button>
          </a-form-item>
        </a-form>
      </a-col>
      <a-col :md="24" :lg="8" :style="{ minHeight: '180px' }">
        <!-- <div class="ant-upload-preview" @click="$refs.modal.edit(user.id, user.avatar)" > -->
        <div class="ant-upload-preview">
          <a-icon type="cloud-upload-o" class="upload-icon"/>
          <div class="mask">
            <a-icon type="plus" />
          </div>
          <img :src="option.img"/>
        </div>
      </a-col>
    </a-row>
    <avatar-modal ref="modal" @ok="setavatar" @cancel="$refs.modal.close()"/>
  </div>
</template>

<script>
  import { postAction } from '@/api/manage'
  import { mapActions, mapState } from 'vuex'
  import moment from 'moment'
  import pick from 'lodash.pick'
  import AvatarModal from './AvatarModal'

  export default {
    name: 'BaseSetting',
    components: {
      AvatarModal
    },
    data () {
      return {
        // cropper
        preview: {},
        option: {
          img: require('@/assets/images/profile.jpg'),
          info: true,
          size: 1,
          outputType: 'jpeg',
          canScale: true,
          autoCrop: true,
          // 只有自动截图开启 宽度高度才生效
          autoCropWidth: 180,
          autoCropHeight: 180,
          fixedBox: true,
          // 开启宽度和高度比例
          fixed: true,
          fixedNumber: [1, 1]
        },
        validatorRules: {
          officeTel: [{ pattern: /^0\d{2,3}-[1-9]\d{6,7}$/, message: '请输入正确的座机号码' }],
          fax: [{ pattern: /^0\d{2,3}-[1-9]\d{6,7}$/, message: '请输入正确的座机号码' }]
        },
        user: {},
        url: {
          base: '/sys/user/base/',
          avatar: '/sys/user/avatar/'
        },
        form: this.$form.createForm(this)
      }
    },
    created () {
      this.handleOk()
    },
    computed: {
      ...mapState({
        userInfo: state => state.user.info
      })
    },
    methods: {
      ...mapActions(['RefreshInfo']),
      handleOk () {
        this.$nextTick(() => {
          this.user = this.userInfo
          if (this.user.avatar) {
            this.option.img = this.user.avatar
          }
          this.user.password = ''
          if(this.user.birthday){
            this.user.birthday = moment(new Date(this.user.birthday), 'YYYY-MM-DD')
          }
          console.log(this.user)
          this.form.setFieldsValue(pick(this.user, 'name', 'nameEn', 'remark', 'officeTel', 'fax', 'birthday', 'sex'))
        })
      },
      setavatar (url) {
        this.option.img = url
        postAction(this.url.avatar, { avatar: url }).then(res => {
          if (res.code == 200) {
            this.option.img = url
            this.user.avatar = url
            this.RefreshInfo()
          }
        })
      },
      base () {
        this.form.validateFields((err, values) => {
          if (!err) {
            postAction(this.url.base, values).then(res => {
              if (res.code == 200) {
                if (res.success) {
                  this.RefreshInfo()
                  this.$message.success(res.message)
                } else {
                  this.$message.warning(res.message)
                }
              }
            })
          }
        })
      }
    }
  }
</script>

<style lang="less" scoped>

  .avatar-upload-wrapper {
    height: 200px;
    width: 100%;
  }

  .ant-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 100%;
    max-width: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 1.4rem;
      padding: 0.5rem;
      background: rgba(222, 221, 221, 0.7);
      border-radius: 50%;
      border: 1px solid rgba(0, 0, 0, 0.2);
    }
    .mask {
      opacity: 0;
      position: absolute;
      background: rgba(0,0,0,0.4);
      cursor: pointer;
      transition: opacity 0.4s;

      &:hover {
        opacity: 1;
      }

      i {
        font-size: 2rem;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -1rem;
        margin-top: -1rem;
        color: #d6d6d6;
      }
    }

    img, .mask {
      width: 100%;
      max-width: 180px;
      height: 180px;
      border-radius: 50%;
      overflow: hidden;
    }
  }
</style>
